HTML forms are essential components for collecting user input on web pages. They enable users to submit data to web servers for processing, making them fundamental for interactive web applications.
The basic structure of an HTML form consists of the <form> tag containing various input elements:
<form action="/submit" method="post"> <!-- Form elements go here --> <input type="submit" value="Submit"> </form>
| Attribute | Description | Values | Example |
|---|---|---|---|
| action | Specifies where to send the form data when submitted | URL or server endpoint | action="/submit-data" |
| method | Defines the HTTP method used to send form data | get or post |
method="post" |
| enctype | Specifies how form data should be encoded | application/x-www-form-urlencodedmultipart/form-datatext/plain |
enctype="multipart/form-data" |
| target | Specifies where to display the response | _blank, _self, _parent, _top |
target="_blank" |
| autocomplete | Controls browser autocomplete functionality | on or off |
autocomplete="off" |
| novalidate | Prevents browser validation | novalidate |
novalidate |
| name | Specifies the name for JavaScript reference | Any string | name="contactForm" |
<input> - Various types for different data inputs<textarea> - Multi-line text input<select> - Dropdown selection<button> - Clickable button<label> - Label for form controls<fieldset> - Groups related elements<legend> - Caption for fieldset<optgroup> - Groups options in dropdown<option> - Options in dropdown<datalist> - Predefined options for inputtext - Single-line textpassword - Masked text inputemail - Email address validationurl - URL validationtel - Telephone numbersearch - Search fieldnumber - Numeric inputrange - Slider controlcolor - Color pickerdate - Date pickertime - Time pickerdatetime-local - Local date and timemonth - Month and yearweek - Week and yearcheckbox - Multiple selectionradio - Single selection from groupfile - File uploadhidden - Hidden fieldsubmit - Form submissionreset - Reset form valuesbutton - Generic buttonimage - Image as submit buttonrequired - Field must be filledpattern - Regex pattern validationmin/max - Minimum/maximum valuesminlength/maxlength - Text length limitsstep - Increment steps for numbers<label> elements for all form controls<fieldset><form action="/contact" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea> <button type="submit">Send Message</button> </form>
<form action="/register" method="post" novalidate>
<fieldset>
<legend>Personal Information</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username"
pattern="[a-zA-Z0-9_]+" minlength="3" maxlength="20" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password"
minlength="8" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="18" max="120" required>
</fieldset>
<fieldset>
<legend>Preferences</legend>
<label for="newsletter">Subscribe to newsletter:</label>
<input type="checkbox" id="newsletter" name="newsletter">
<label>Notification frequency:</label>
<select name="frequency">
<option value="daily">Daily</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
</select>
</fieldset>
<button type="submit">Register</button>
<button type="reset">Clear Form</button>
</form>
<form action="/upload" method="post" enctype="multipart/form-data"> <label for="file">Select file to upload:</label> <input type="file" id="file" name="file" accept=".jpg,.png,.pdf"> <label for="description">File description:</label> <input type="text" id="description" name="description"> <button type="submit">Upload File</button> </form>